package com.yh.services.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yh.dao.PicMapper;
import com.yh.entity.Pic;
import com.yh.services.PicService;
import com.yh.util.FileUtil;
import com.yh.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import sun.plugin.util.UIUtil;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Service
public class PicServiceImpl implements PicService {
    @Value("${web.upload-path}")
    private String path;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private PicMapper picMapper;

    @Override
    public Integer addPic(MultipartFile file, Pic pic) {
        String name= UUIDUtil.getUUID()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

        try {
            byte[] _file=file.getBytes();
            FileUtil.doUpload(_file,path,name);
            pic.setPic(name);
            pic.setStatus(0);
            return picMapper.insert(pic);
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public Page<Pic> getAllPic(Integer flag, Pic pic, Integer pageIndex, Integer pageSize) {
        Page<Pic> page=new Page<>(pageIndex,pageSize);
        if (flag==1){
            page.setRecords(picMapper.getUpPic(pic,page));
        }else {
            page.setRecords(picMapper.getAllPic(pic,page));
        }

        return page;
    }

    @Override
    public Pic getPicById(Integer id) {
        return  picMapper.selectById(id);
    }

    @Override
    public Integer updateStatus(Pic pic) {
        redisTemplate.delete("pic");
        return picMapper.updateById(pic);
    }

    @Override
    public Integer updatePic(MultipartFile file, Pic pic) {
        if (file.getSize()!=0){
            String name= UUIDUtil.getUUID()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

            try {
                byte[] _file = file.getBytes();
                FileUtil.doUpload(_file,path,name);
            } catch (IOException e) {
                e.printStackTrace();
            }
            pic.setPic(name);
        }
        redisTemplate.delete("pic");
        return picMapper.updateById(pic);
    }

    @Override
    public List<Pic> getPic() {
        ListOperations<String,Pic> ops=redisTemplate.opsForList();
        System.out.println("======从缓存找======");
        List<Pic> list=ops.range("pic",0,-1);
        if (list==null||list.isEmpty()){
            Page<Pic> page =new Page<>(1,5);
            System.out.println("======从数据库找======");
            list=picMapper.getPic(page);
            ops.rightPushAll("pic",list);
        }

        return list;
    }
}
